/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dal;

import connection.ConnectionDB;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Places;

/**
 *
 * @author Administrator
 */
public class PlacesDAL {

    private ConnectionDB conn = new ConnectionDB();
    private CallableStatement cs;

    public boolean newPlaces(Places place) {
        try {
            cs = conn.createConnection().prepareCall("{call sp_NewPlace(?,?,?)}");
            cs.setInt("locationID", place.getLocationID());
            cs.setString("name", place.getPlaceName());
            cs.setString("description", place.getDescription());

            if (cs.executeUpdate() == 1) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(PlacesDAL.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public boolean updatePlaces(Places place) {
        try {
            cs = conn.createConnection().prepareCall("{call sp_UpdatePlace(?,?,?,?)}");
            cs.setInt("placeID", place.getPlaceID());
            cs.setInt("locationID", place.getLocationID());
            cs.setString("name", place.getPlaceName());
            cs.setString("description", place.getDescription());

            if (cs.executeUpdate() == 1) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(PlacesDAL.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public List<Places> listPlacesByLocalID(int localID){
        try {
            List<Places> list = new ArrayList<Places>();
            Places place;
            cs = conn.createConnection().prepareCall("{call sp_ListPlaceByLocationID(?)}");
            cs.setInt("locationID", localID);
            ResultSet rs= cs.executeQuery();

            while(rs.next()){
                place = new Places(rs.getInt("PlaceID"),rs.getInt("LocationID"),rs.getString("PlaceName"),rs.getString("Description"));
                list.add(place);
            }
            return list;
        } catch (SQLException ex) {
            Logger.getLogger(PlacesDAL.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public List<Places> listAllPlace(){
        try {
            List<Places> list = new ArrayList<Places>();
            Places place;
            cs = conn.createConnection().prepareCall("{call sp_ListPlace}");
            ResultSet rs = cs.executeQuery();

            while(rs.next()){
                place = new Places(rs.getInt("PlaceID"),rs.getInt("LocationID"),rs.getString("PlaceName"),rs.getString("Description"));
                list.add(place);
            }
            return list;
        } catch (SQLException ex) {
            Logger.getLogger(PlacesDAL.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }
}
